package com.htfyun.hardware;

import android.content.ContentResolver;
import android.content.Context;
import android.provider.Settings;
import android.util.Log;

import com.htfyun.hardware.reflect.Utils;

public final class AssistUtils {

    private final static String TAG = "AssistUtils";
    private final static String ADB_ENABLED = Settings.Global.ADB_ENABLED;

    private static final String ACCESSIBILITY_DISPLAY_DALTONIZER_ENABLED =
            "accessibility_display_daltonizer_enabled";
    private static final String ACCESSIBILITY_DISPLAY_DALTONIZER =
            "accessibility_display_daltonizer";
    private static final int DALTONIZER_SIMULATE_MONOCHROMACY = 0;

    private static final String PROPERTY_SET_TIME_ENABLED = "persist.set_time.enabled";
    private static final String PROPERTY_HARD_DISK_SCAN_ENABLED = "persist.hd.scan_enabled";

    public static boolean setADBEnabled(Context context, boolean enabled) {
        if (context == null) {
            return false;
        }
        ContentResolver cr = context.getContentResolver();
        try {
            return Settings.Global.putInt(cr, ADB_ENABLED, enabled ? 1 : 0);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;

    }

    public static boolean isADBEnabled(Context context) {
        if (context == null) {
            return false;
        }
        ContentResolver cr = context.getContentResolver();
        try {
            int value = Settings.Global.getInt(cr, ADB_ENABLED, 0);
            Log.e(TAG, "isADBEnabled value = " + value);
            return value != 0;
        } catch (Exception e) {
        }

        return false;
    }

    ////////////////
    public static void setModifyTimeEnabled(boolean enabled) {
        Utils.setSystemProperty(PROPERTY_SET_TIME_ENABLED, enabled ? "true" : "false");
    }

    public static boolean isModifyTimeEnabled() {
        String enabled = Utils.getSystemProperty(PROPERTY_SET_TIME_ENABLED);
        return enabled == null || enabled.length() == 0
                || enabled.equalsIgnoreCase("true")
                || enabled.equalsIgnoreCase("1")
                || enabled.equalsIgnoreCase("yes")
                || enabled.equalsIgnoreCase("y")
                || enabled.equalsIgnoreCase("on");
    }

    // 设置系统是否扫描硬盘, 系统扫描硬盘时, 会去读硬盘, 导致硬盘电流大.
    // 如果不扫描的话, MTP 不会去显示硬盘内容.
    public static void setHardDiskScanEnabled(boolean enabled) {
        Utils.setSystemProperty(PROPERTY_HARD_DISK_SCAN_ENABLED, enabled ? "1" : "0");
    }

    public static boolean isHardDiskScanEnabled() {
        String enabled = Utils.getSystemProperty(PROPERTY_HARD_DISK_SCAN_ENABLED);
        return enabled == null || enabled.length() == 0
                || enabled.equalsIgnoreCase("true")
                || enabled.equalsIgnoreCase("1")
                || enabled.equalsIgnoreCase("yes")
                || enabled.equalsIgnoreCase("y")
                || enabled.equalsIgnoreCase("on");
    }

}
